package sg.edu.nus.dbd.biz;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import sg.edu.nus.dbd.dao.ConfigurationDAO;
import sg.edu.nus.dbd.dao.DAOException;
import sg.edu.nus.dbd.dao.DAOFactory;
import sg.edu.nus.dbd.dao.DashboardDAO;
import sg.edu.nus.dbd.formbean.ChartData;
import sg.edu.nus.dbd.formbean.ChartDataItem;
import sg.edu.nus.dbd.formbean.DetailParamData;
import sg.edu.nus.dbd.formbean.ThresholdConfig;
import sg.edu.nus.dbd.util.Constants;

public class DashboardBiz {

	public ChartData getReportData(String paramName, Date startDate,
			Date endDate) {
		ChartData reportData = new ChartData();
		try {
			ConfigurationDAO conDao = DAOFactory.getInstance()
					.getConfigurationDAO();
			ChartDataItem item = new ChartDataItem();
			List<ChartDataItem> lstItems = new ArrayList<ChartDataItem>();
			ThresholdConfig configHelper = conDao
					.getThresholdParameter(paramName);

			String description = "Report Data for " + paramName;

			reportData.setChartDescription(description);

			double averageValue = 0;
			try {
				DashboardDAO ddao = DAOFactory.getInstance().getDashboardDAO();
				averageValue = ddao.getLevel1Statistics(paramName, startDate,
						endDate);
			} catch (DAOException e) {
				e.printStackTrace();
			}

			if (configHelper != null) {
				double minValue = configHelper.getMinLevel();
				double maxValue = configHelper.getMaxLevel();

				item.setParameterGroup("");
				item.setUnit("");
				item
						.setParameterName((String) Constants.DASHBOARD_PARAMS_METRIC
								.get(paramName));
				item.setAverageValue(averageValue);

				item.setMinValue(minValue);
				item.setGreenMinValue(minValue);
				item.setGreenMaxValue(configHelper.getWarningLevel());
				item.setYellowMinValue(configHelper.getWarningLevel());
				item.setYellowMaxValue(configHelper.getCriticalLevel());
				item.setRedMinValue(configHelper.getCriticalLevel());
				item.setRedMaxValue(maxValue);
				item.setMaxValue(maxValue);

				lstItems.add(item);
			}

			reportData.setReportDataItems(lstItems);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return reportData;
	}

	public DetailParamData getLevel1Statistics(String paramName,
			Date startDate, Date endDate) {
		try {
			DashboardDAO dbDao = DAOFactory.getInstance().getDashboardDAO();
			// Set the interval to 1 day
			return new DetailParamData(startDate, endDate, dbDao
					.getLevel1Statistics(paramName, startDate, endDate));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<DetailParamData> getLevel2Statistics(String paramName,
			Date startDate, Date endDate) {
		try {
			DashboardDAO dbDao = DAOFactory.getInstance().getDashboardDAO();
			// Set the interval to 1 day
			return dbDao.getBreakdownStatistics(paramName, startDate, endDate,
					60 * 24);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<DetailParamData> getLevel3Statistics(String paramName,
			Date startDate, Date endDate) {
		try {
			DashboardDAO dbDao = DAOFactory.getInstance().getDashboardDAO();
			// Set the interval to 1 hour
			return dbDao.getBreakdownStatistics(paramName, startDate, endDate,
					60);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<DetailParamData> getODReportData(String paramName,
			int intervalInMin, Date startDate, Date endDate) {
		try {
			DashboardDAO dbDao = DAOFactory.getInstance().getDashboardDAO();
			return dbDao.getBreakdownStatistics(paramName, startDate, endDate,
					intervalInMin);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
